package hsp.admin.app.base.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import hsp.admin.api.ApiResult;
import hsp.admin.app.base.po.MenuPO;
import hsp.admin.app.base.service.IMenuService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;


@Tag(name = "MenuController", description = "菜单管理")
@RestController
@RequestMapping("base/menu")
public class MenuController {

    private final IMenuService service;

    @Autowired
    public MenuController(IMenuService service){ this.service = service; }

    @Operation(summary = "获取菜单")
    @GetMapping("{id}")
    public ApiResult<MenuPO> getMenu(@PathVariable("id") String id){
        MenuPO po = service.getById(id);
        return ApiResult.success(po);
    }

    @Operation(summary = "根据程序主键获取菜单列表")
    @GetMapping("list/{appId}")
    public ApiResult<List<MenuPO>> getMenuListByAppId(@PathVariable String appId){
        LambdaQueryWrapper<MenuPO> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(MenuPO::getAppId, appId);
        List<MenuPO> records = service.list(wrapper);
        return ApiResult.success(records);
    }

    @Operation(summary = "编辑菜单")
    @PostMapping()
    public ApiResult<?> postAndPutMenu(@RequestBody MenuPO po){
        service.saveOrUpdate(po);
        return ApiResult.success();
    }

    @Operation(summary = "删除菜单")
    @DeleteMapping("{id}")
    public ApiResult<?> deleteMenu(@PathVariable("id") String id){
        service.removeById(id);
        return ApiResult.success();
    }

}
